package com.lightningtrade.controller;

import com.lightningtrade.domain.SysOrder;
import com.lightningtrade.log.annotaion.Log;
import com.lightningtrade.log.enums.BusinessType;
import com.lightningtrade.service.SysOrderService;
import com.lightningtrade.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

@Api(tags = "订单管理-0312杜晟玮")
@RestController
@RequestMapping("/order")
public class SysOrderController {

    @Autowired
    private SysOrderService sysOrderService;

    /**
     * 创建订单
     * @param params 订单信息
     * @return AjaxResult
     */
    @ApiOperation(value = "创建订单", notes = "根据用户信息和商品信息创建订单")
    @PostMapping("/create")
    public AjaxResult createOrder(@RequestBody Map<String, Object> params) {
        // 从 params 中获取参数
        Integer productId = Integer.valueOf(params.get("productId").toString());  // 商品编号
        Integer userId = Integer.valueOf(params.get("userId").toString());  // 用户编号
        Integer quantity = Integer.valueOf(params.get("quantity").toString());  // 商品数量
        Double totalPrice = Double.valueOf(params.get("totalPrice").toString());  // 总金额

        // 构建 SysOrder 对象
        SysOrder order = new SysOrder();
        order.setProductId(productId);  // 设置商品编号
        order.setUid(userId);  // 设置用户编号
        order.setProductQuantity(quantity);  // 设置商品数量
        order.setTotalPrice(totalPrice);  // 设置总金额
        // 你可以设置其他的默认值，比如订单状态等
        order.setCompleteStatus(0);  // 默认状态为执行中
        order.setPaymentStatus(0);  // 默认支付状态为未支付
        order.setCancelStatus(0);  // 默认取消状态为未取消

        // 调用 service 层创建订单
        return sysOrderService.createOrder(order);
    }


    /**
     * 查询订单
     * @param id 订单编号
     * @return AjaxResult
     */
    @ApiOperation(value = "查询订单", notes = "根据订单编号查询订单")
    @GetMapping("/get/{id}")
    public AjaxResult getOrderById(@PathVariable Long id) {
        return sysOrderService.getOrderById(id);
    }

    /**
     * 取消订单
     * @param id 订单编号
     * @return AjaxResult
     */
    @ApiOperation(value = "取消订单", notes = "取消订单并还原库存")
    @PostMapping("/cancel/{id}")
    public AjaxResult cancelOrder(@PathVariable Long id) {
        return sysOrderService.cancelOrder(id);
    }

    /**
     * 支付订单
     * @param id 订单编号
     * @return AjaxResult
     */
    @Log(title = "支付订单", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "支付订单", notes = "支付订单并减少库存")
    @PostMapping("/pay/{id}")
    public AjaxResult payOrder(@PathVariable Long id) {
        return sysOrderService.payOrder(id);
    }
}
